{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "24eeddf1-b41a-4286-a610-46289d055983",
   "metadata": {},
   "source": [
    "# 一、创建多维数组ndarray\n",
    "- ndarray: 多维数组，具有矢量运算能力，且快速，节省空间\n",
    "- 可对整组数据进行快速运算的表针数学函数，线性代数，随机数生成等功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d319d16c-c578-40ea-8eb2-105ad61526d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr1 = np.array([1,2,3])\n",
    "print(arr1)\n",
    "print(type(arr1))\n",
    "\n",
    "arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "print(arr2)\n",
    "print(type(arr2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "299398f4-f4ff-41db-95a3-6ad77ab7b8d8",
   "metadata": {},
   "source": [
    "# 二、Numpy数组对象ndarray\n",
    "数组属性：ndarray是存储演绎数据类型的多维数组\n",
    "- ndim: 返回int。表示数组的维数\n",
    "- shape: 返回tuple。表示数组的尺寸，对于一维数组，返回列数；对于n行m列的矩阵，形状为（n,m）\n",
    "- size: 返回int。表示数组的元素总数，等于数组形状的乘积。\n",
    "- dtype: 返回data-type。描述数组中元素的类型。\n",
    "- itemsize: 返回int。表示数组的每个元素的大小（以字节为单位）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "019498a1-7ad6-45a0-9e55-2dc5f156aea3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "(3,)\n",
      "3\n",
      "int32\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "print(arr1.ndim)\n",
    "print(arr1.shape)\n",
    "print(arr1.size)\n",
    "print(arr1.dtype)\n",
    "print(arr1.itemsize)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e20ffd3-7d8d-405b-90c7-339e6b0f4147",
   "metadata": {},
   "source": [
    "# 三、认识数组与列表的区别\n",
    "- 数组具有适量运算能力"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "683e0944-4edd-47fe-8d5f-a6b566c716b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9]\n",
      "[1 4 9]\n"
     ]
    }
   ],
   "source": [
    "ls = [1,2,3]\n",
    "arr = np.array(ls)\n",
    "# 现在将数组的每个元素都做平方运算\n",
    "# ls**2 报错\n",
    "arr**2 # 正常运行，结果正确\n",
    "\n",
    "print([i**2 for i in ls])\n",
    "print(arr**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "352cbbbb-1818-4eb7-b0e5-c6bc0636eae2",
   "metadata": {},
   "source": [
    "# 四、生成等差数列\n",
    "- np.arange(start, stop, step) 左闭右开，以step为步长，生成等差数列\n",
    "- np.linspace(1, 10, 20),  有闭右闭， 一共生成20个数的等差数列\n",
    "- np.zeros([3,4]),创建全0数组, 3行4列\n",
    "- np.ones([3,4]), 创建全1数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d7d1c320-fd14-45d9-844d-f84033127992",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "[ 0.          0.52631579  1.05263158  1.57894737  2.10526316  2.63157895\n",
      "  3.15789474  3.68421053  4.21052632  4.73684211  5.26315789  5.78947368\n",
      "  6.31578947  6.84210526  7.36842105  7.89473684  8.42105263  8.94736842\n",
      "  9.47368421 10.        ]\n",
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = np.arange(0, 10, 2)\n",
    "arr3 = np.linspace(0,10, 20)\n",
    "print(arr2)\n",
    "print(arr3)\n",
    "arr4 = np.ones([3,4])\n",
    "print(arr4)\n",
    "# help(np.ones)\n",
    "# print(np.zeros())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f5e3f6d-29b6-4db7-b292-9122c0dce6b7",
   "metadata": {},
   "source": [
    "## 4.1 help(np.arange)\n",
    "- 查看某个函数的用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "23383de1-6dfe-4499-83da-5d55010248f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# help(np.arange)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b201d546-dce9-49f9-8c32-0bb1730963ba",
   "metadata": {},
   "source": [
    "# 五、数据类型\n",
    "- 数据类型向下取整，取小于x的大整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c60b5fc7-3563-48e0-8852-d7cd4547c1b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.49999 3.5     4.6    ]\n",
      "float32\n",
      "[2 3 4]\n"
     ]
    }
   ],
   "source": [
    "arr5 = np.array([2.49999,3.5,4.6], dtype=np.float32) # 在创建数组时生命其数据类型\n",
    "print(arr5)\n",
    "print(arr5.dtype)\n",
    "\n",
    "print(np.int32(arr5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d25bf308-1f79-43b5-8ece-6ad8fd9ea654",
   "metadata": {},
   "source": [
    "## 5.1 生成随机数组\n",
    "- np.random.random(n): 在[0,1)中间，生成n个随机数(无约束条件)\n",
    "- np.random.rand(n): 在[0,1)之间，生成n个均匀分布的随机数\n",
    "- np.random.rand(3,4): 生成指定shape的均匀分布随机数，3行4列\n",
    "- np.random.randn(3，4，5): 生成指定shape的正态分布的随机数，3行4列5层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e881e69e-128e-44ad-9772-f110925f80d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.13532866 0.33790302 0.08069066 0.40388787 0.74054608 0.77565488\n",
      " 0.63908098 0.24442683 0.861735   0.03753312]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0.3430514 , 0.5328308 , 0.53160321, 0.30438698, 0.56901313,\n",
       "       0.51206742, 0.14352741, 0.43863306, 0.3776596 , 0.47765155])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(np.random.random(10))\n",
    "np.random.rand(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0f221b8e-eeb5-46e2-9263-51d19172c99c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# help(np.random.rand)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ac93642-4f17-4a67-b90d-b09f94fcbc91",
   "metadata": {},
   "source": [
    "## 5.2 random其他函数\n",
    "- seed: 确定随机生成器的种子。\n",
    "- permutation: 返回一个序列的随机排列，或返回一个随机排列的范围。\n",
    "- shuffle: 对一个序列进行随机排序。\n",
    "- binomial: 产生一个二项分布的随机数。\n",
    "- normal: 产生一个正态（高斯）分布的随机数。\n",
    "- beta: 产生beta分布的随机数。\n",
    "- chisquare: 产生卡方分布的随机数。\n",
    "- gamma: 产生gamma分布的随机数。\n",
    "- uniform: 产生在[0,1)中随机分布的随机数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "534fac8b-1b05-42da-8fec-76a496daf5a8",
   "metadata": {},
   "source": [
    "## 5.3 逻辑索引\n",
    "### 5.3.1 一维数组的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "e36780c4-64be-447c-a1e6-94fb9d697137",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3]\n",
      "[False False  True]\n",
      "[3]\n"
     ]
    }
   ],
   "source": [
    "# 数组的逻辑形索引--不常用\n",
    "ls = [1,2,3]\n",
    "arr_ls = np.array(ls)\n",
    "print(arr_ls[[True, False, True]])\n",
    "\n",
    "# 常用\n",
    "index_list = arr_ls>2 # 将数组 ls 中的每一个元素都拿出来和 2 比较，并返回一个布尔值列表\n",
    "print(index_list)\n",
    "print(arr_ls[index_list])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27979b0a-3ee0-4a85-997e-9a31ddd4338e",
   "metadata": {},
   "source": [
    "### 5.3.2 多维数组的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "37701029-43ba-4195-83df-8964145ef397",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "12\n",
      "[ 9 10 11 12]\n",
      "[ 3  7 11]\n",
      "[[ 6  7]\n",
      " [10 11]]\n",
      "[10]\n",
      "[[ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "# arange(i，j), 左闭右开生成 (i-j) 个数字，和range差不多\n",
    "# reshape(), 重新调整数组的形状，维数，几行几列\n",
    "arr = np.arange(1,13).reshape([3,4])\n",
    "print(arr)\n",
    "print(arr[2,3])  # 访问第2行第3列的元素12\n",
    "print(arr[2,:])  # 访问第2行的所有数据\n",
    "print(arr[:,2])  # 返回第2列的所有数据\n",
    "print(arr[1:3, 1:3])  # 返回指定行指定列的元素列表，第1行到第2行和第1到第2列的元素数组\n",
    "print(arr[2:, 1])  # 取第二行到末尾行，和第1列的元素数组\n",
    "print(arr[arr[:,0]>4, :])  # 多维数组的逻辑索引，取第0列大于4的行，每行取所有列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6c561e6-4cc5-492a-aff0-6c30c4697338",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "200beb6c-49f8-41ee-85b5-b3fe153a739e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
